<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admin_controller extends MY_Controller
{
    public $page_title = 'Quản trị nội dung';
    public $table_name = '';
    public $route_name = '';
    public $business_model ;
    public $save_notification = 'Hoàn tất: Bạn đã sửa đổi dữ liệu!';
    public $can_upload_image = false;
    public $base_admin_url = 'ci-admin/';

    public function __construct()
    {
        parent::__construct();
        //if(!$this->can_this_user_access_this_page()) redirect(base_url().'ci-admin');
    }

    public function can_this_user_access_this_page()
    {
        return $this->is_admin_logged();
    }


    public function set_admin_logged($user)
    {
        $this->session->set_userdata('admin_logged',$user);
    }

    public function unset_admin_logged()
    {
        $this->session->unset_userdata('admin_logged');
    }

    public function is_admin_logged()
    {
        return $this->session->userdata('admin_logged') !== false;
    }

    public function get_logged_admin_account()
    {
        return $this->session->userdata('admin_logged');
    }

    public function index()
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['list'] = $this->get_list();
        $view_data['actions'] = $this->get_list_actions();
        $this->load_view('list_template',$header_data,$view_data);
    }

    public function update($id)
    {
        $header_data = array();
        $header_data['show_menu'] = true;
        $view_data = $this->get_view_data();
        $view_data['fields'] = $this->get_fields($id);
        $view_data['actions'] = $this->get_form_actions($id);
        $this->load_view('form_template',$header_data,$view_data);
    }

    public function delete()
    {
        if($this->input->post('selected') === false) redirect(base_url().$this->base_admin_url.$this->route_name);
        $selected = $this->input->post('selected');
        foreach($selected as $id)
        {
            // Delete news
            $this->db->delete($this->table_name,array('id' => $id));
        }
        $this->session->set_userdata('message','Hoàn tất: Bạn đã xóa dữ liệu!');
        redirect(base_url(). $this->base_admin_url .$this->route_name);
    }

    public function save($districtid)
    {
        $post_data = $this->get_post_data();
        if($districtid == -1)
        {
            $this->db->insert($this->table_name,$post_data);
            $districtid = $this->db->insert_id();
        }
        else
        {
            $this->db->update($this->table_name,$post_data,array('id' => $districtid));
        }
        $this->session->set_userdata('message',$this->save_notification);
        redirect($this->hook_redirect_url_after_save($districtid));
    }

    public function get_view_data()
    {
        $view_data = array();
        $view_data['breadcrumb'] = $this->hook_breadcrumb();
        $view_data['title'] = $this->page_title;
        $view_data['headers'] = $this->get_header_list();
        $view_data['can_upload_image'] = $this->can_upload_image;
        if($this->session->userdata('message') != false)
        {
            $view_data['message'] = $this->session->userdata('message');
            $this->session->unset_userdata('message');
        }
        return $view_data;
    }

    public function hook_breadcrumb()
    {
        $bc = array();
        $bc[] = array(
            'href' => base_url(). $this->base_admin_url . 'user',
            'text' => 'Trang chủ',
            'last' => false
        );
        $bc[] = array(
            'href' => base_url(). $this->base_admin_url .$this->route_name,
            'text' => $this->page_title,
            'last' => true
        );
        return $bc;
    }

    public function get_list_actions()
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Thêm',
            'href' => base_url(). $this->base_admin_url .$this->route_name.'/update/-1',
            'view_change_method' => 'redirect'
        );
        $actions[] = array(
            'text' => 'Xóa',
            'href' => base_url(). $this->base_admin_url .$this->route_name.'/delete',
            'view_change_method' => 'submit'
        );
        return $actions;
    }

    public function get_form_actions($id)
    {
        $actions = array();
        $actions[] = array(
            'text' => 'Lưu',
            'href' => base_url(). $this->base_admin_url .$this->route_name.'/save/'.$id,
            'view_change_method' => 'submit'
        );
        $actions[] = array(
            'text' => 'Hủy',
            'href' => $this->hook_cancel_url($id),
            'view_change_method' => 'redirect'
        );
        return $actions;
    }

    public function get_post_data()
    {
        return array();
    }

    public function get_header_list()
    {
        return array();
    }

    public function get_list()
    {
        return array();
    }

    public function hook_redirect_url_after_save($id)
    {
        return base_url(). $this->base_admin_url .$this->route_name;
    }

    public function hook_cancel_url($id)
    {
        return base_url(). $this->base_admin_url .$this->route_name;
    }

    // UTIL FUNCTION
    // Input :  + input_file_name : name of html file input
    //          + old_file_name : full name of old image to be deleted.
    //          + upload_path : the path to upload image.
    // Return:  full name of new uploaded image.
    public function upload_image($input_file_name,$old_file_name = '',$upload_path='uploads/')
    {
        $upload_config['upload_path'] = $upload_path;
        $upload_config['allowed_types'] = '*';
        $upload_config['file_name'] = time();
        $this->upload->initialize($upload_config);

        if($this->upload->do_upload($input_file_name))
        {
            if($old_file_name != '') $this->delete_file($old_file_name);
            $upload_data = $this->upload->data();
            $uploaded_file_name = $upload_path . $upload_data['file_name'];
        }
        else
        {
            $uploaded_file_name = $old_file_name;
        }

        return $uploaded_file_name;
    }

    // UTIL FUNCTION
    public function delete_file($file_path)
    {
        if(is_writeable($file_path) && is_file($file_path)) unlink($file_path);
    }

    public function load_view($view,$header_data,$view_data)
    {
        $header_data['base_admin_url'] = $this->base_admin_url;
        $view_data['base_admin_url'] = $this->base_admin_url;
        $this->load->view('admin/master_page/header',$header_data);
        $this->load->view('admin/'.$view,$view_data);
        $this->load->view('admin/master_page/footer');
    }


}